#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void SeqListInit(SeqList* ps)
{
	assert(ps);
	ps->a = (SLDateType*)malloc(sizeof(SLDateType) * 4);
	ps->size = 0;
	ps->capacity = 4;
}

void SeqListDestroy(SeqList* ps)
{
	assert(ps);
	assert(ps->a);
	free(ps->a);
	ps->size = 0;
	ps->capacity = 0;
	ps = NULL;
}

void SeqListPrint(SeqList* ps)
{
	assert(ps);
	assert(ps->a);
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
}

void SeqListCheck(SeqList* ps)
{
	assert(ps);
	assert(ps->a);
	if (ps->size == ps->capacity)
	{
		SLDateType* tmp = realloc(ps->a, (ps->capacity) * 2 * sizeof(SeqList));
		if (!tmp)
		{
			printf("SeqList fail\n");
			exit(-1);
		}
		ps->capacity *= 2;
		ps->a = tmp;
	}
}

void SeqListPushBack(SeqList* ps, SLDateType x)
{
	assert(ps);
	assert(ps->a);
	SeqListCheck(ps);
	ps->a[ps->size++] = x;
}

void SeqListPushFront(SeqList* ps, SLDateType x)
{
	assert(ps);
	assert(ps->a);
	SeqListCheck(ps);
	int i = 0;
	for (i = ps->size;i>0; i--)
	{
		ps->a[i] = ps->a[i - 1];
	}
	ps->a[0] = x;
	ps->size++;
}

void SeqListPopFront(SeqList* ps)
{
	assert(ps);
	assert(ps->a);
	int i = 0;
	for (i = 0; i < ps->size - 1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}

void SeqListPopBack(SeqList* ps)
{
	assert(ps);
	assert(ps->a);
	ps->size--;
}

int SeqListFind(SeqList* ps, SLDateType x)
{
	assert(ps);
	assert(ps->a);
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		return i;
	}
	return 0;
}

void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
	assert(ps);
	assert(ps->a);
	assert(pos <= ps->size&& pos >= 0);
	SeqListCheck(ps);
	int i = 0;
	for (i = ps->size; i > pos; i--)
	{
		ps->a[i] = ps->a[i - 1];
	}
	ps->a[pos] = x;
	ps->size++;
}

void SeqListErase(SeqList* ps, int pos)
{
	assert(ps);
	assert(ps->a);
	assert(pos < ps->size&& pos >= 0);
	int i = 0;
	for (i = pos; i < ps->size - 1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}
